package com.lightningtrade.service.impl;

import com.lightningtrade.domain.SysReview;
import com.lightningtrade.mapper.SysReviewMapper;
import com.lightningtrade.service.SysReviewService;
import com.lightningtrade.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysReviewServiceImpl implements SysReviewService {

    @Autowired
    private SysReviewMapper sysReviewMapper;

    @Override
    public AjaxResult addReview(SysReview sysReview) {
        try {
            int result = sysReviewMapper.insertReview(sysReview);
            if (result > 0) {
                return AjaxResult.success("评价添加成功");
            } else {
                return AjaxResult.error("评价添加失败");
            }
        } catch (Exception e) {
            return AjaxResult.error("系统错误: " + e.getMessage());
        }
    }

    @Override
    public AjaxResult getReviewsByUser(int userId) {
        try {
            List<SysReview> reviews = sysReviewMapper.selectReviewsByUser(userId);
            if (reviews != null && !reviews.isEmpty()) {
                return AjaxResult.success("获取评价成功", reviews);
            } else {
                return AjaxResult.success("无评价记录", reviews);
            }
        } catch (Exception e) {
            return AjaxResult.error("系统错误: " + e.getMessage());
        }
    }

    @Override
    public AjaxResult getReviewsByProduct(int productId) {
        try {
            List<SysReview> reviews = sysReviewMapper.selectReviewsByProduct(productId);
            if (reviews != null && !reviews.isEmpty()) {
                return AjaxResult.success("获取评价成功", reviews);
            } else {
                return AjaxResult.success("无评价记录", reviews);
            }
        } catch (Exception e) {
            return AjaxResult.error("系统错误: " + e.getMessage());
        }
    }

    @Override
    public AjaxResult getReviewsByOrder(long orderId) {
        try {
            List<SysReview> reviews = sysReviewMapper.selectReviewsByOrder(orderId);
            if (reviews != null && !reviews.isEmpty()) {
                return AjaxResult.success("获取评价成功", reviews);
            } else {
                return AjaxResult.success("无评价记录", reviews);
            }
        } catch (Exception e) {
            return AjaxResult.error("系统错误: " + e.getMessage());
        }
    }

    @Override
    public AjaxResult deleteReview(int id) {
        try {
            int result = sysReviewMapper.deleteReviewById(id);
            if (result > 0) {
                return AjaxResult.success("评价删除成功");
            } else {
                return AjaxResult.error("评价删除失败");
            }
        } catch (Exception e) {
            return AjaxResult.error("系统错误: " + e.getMessage());
        }
    }
}